package com.gingold.basislibrary.utils

import android.app.AlertDialog
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.NumberPicker
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date

/**
 * time 工具类
 */
class BasisTimesUtils {
    /**
     * 隐藏年, 只显示月和日
     */
    fun setYearGone() {
        setSpecialDatePicker(1)
    }

    /**
     * 隐藏日, 只显示年和月
     */
    fun setDayGone() {
        setSpecialDatePicker(2)
    }

    private fun setSpecialDatePicker(state: Int) {
        try {
            val dp = mDatePickerDialog!!.datePicker
            val view0 =
                ((dp.getChildAt(0) as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(0) as NumberPicker //获取最前一位的宽度
            val view1 =
                ((dp.getChildAt(0) as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(1) as NumberPicker //获取中间一位的宽度
            val view2 =
                ((dp.getChildAt(0) as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(2) as NumberPicker //获取最后一位的宽度

            //年的最大值为2100
            //月的最大值为11
            //日的最大值为28,29,30,31
            val value0 = view0.maxValue //获取第一个View的最大值
            val value1 = view1.maxValue //获取第二个View的最大值
            val value2 = view2.maxValue //获取第三个View的最大值
            if (state == 1) { //隐藏年, 只显示月和日
                if (value0 > 252) {
                    view0.visibility = View.GONE
                } else if (value1 > 252) {
                    view1.visibility = View.GONE
                } else if (value2 > 252) {
                    view2.visibility = View.GONE
                }
            } else if (state == 2) { //隐藏日, 只显示年和月
                if (value0 > 25 && value0 < 252) {
                    view0.visibility = View.GONE
                } else if (value1 > 25 && value1 < 252) {
                    view1.visibility = View.GONE
                } else if (value2 > 25 && value2 < 252) {
                    view2.visibility = View.GONE
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 日期选择器监听
     */
    interface OnDatePickerListener {
        fun onConfirm(year: Int, month: Int, dayOfMonth: Int)
        fun onCancel()
    }

    /**
     * 时间选择器监听
     */
    interface OnTimerPickerListener {
        fun onConfirm(hourOfDay: Int, minute: Int)
        fun onCancel()
    }

    companion object {
        var THEME_DEVICE_DEFAULT_LIGHT = AlertDialog.THEME_DEVICE_DEFAULT_LIGHT
        var THEME_DEVICE_DEFAULT_DARK = AlertDialog.THEME_DEVICE_DEFAULT_DARK
        var THEME_TRADITIONAL = AlertDialog.THEME_TRADITIONAL
        var THEME_HOLO_LIGHT = AlertDialog.THEME_HOLO_LIGHT
        var THEME_HOLO_DARK = AlertDialog.THEME_HOLO_DARK
        private var mDatePickerDialog: DatePickerDialog? = null //日期选择器

        /**
         * 比较两个字符串时间是否符合预期
         */
        fun compareTimes(time1: String, time2: String): Int {
            val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
            val dateTime1 = LocalDateTime.parse(time1, formatter)
            val dateTime2 = LocalDateTime.parse(time2, formatter)
            return dateTime1.compareTo(dateTime2)
        }

        fun isLaterThanCurrentTime(time: String): Boolean {
            val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
            val currentTime = LocalDateTime.now()
            val givenTime = LocalDateTime.parse(time, formatter)
            return givenTime.isAfter(currentTime)
        }

        /**
         * 将字符串时间转为Long时间
         *
         * @param time yyyy-MM-dd HH:mm:ss:SSS
         */
        fun getLongTimeOfSSS(time: String?): Long {
            try {
                val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
                val date = sdf.parse(time)
                return date.time
            } catch (e: Exception) {
            }
            return 0L
        }

        /**
         * 将字符串时间转为Long时间
         *
         * @param time yyyy-MM-dd HH:mm:ss
         */
        fun getLongTime(time: String?): Long {
            try {
                val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                val date = sdf.parse(time)
                return date.time
            } catch (e: Exception) {
            }
            return 0L
        }

        /**
         * 将字符串时间转为Long时间
         *
         * @param time yyyy-MM-dd
         */
        fun getLongTimeOfYMD(time: String?): Long {
            try {
                val sdf = SimpleDateFormat("yyyy-MM-dd")
                val date = sdf.parse(time)
                return date.time
            } catch (e: Exception) {
            }
            return 0L
        }

        /**
         * 将Long时间转成String时间
         *
         * @return yyyy-MM-dd HH:mm:ss:SSS
         */
        fun getStringTimeOfSSS(time: Long?): String {
            val date = Date(time!!)
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
            return sdf.format(date)
        }

        /**
         * 将Long时间转成String时间
         *
         * @return yyyy-MM-dd HH:mm:ss
         */
        fun getStringTime(time: Long?): String {
            val date = Date(time!!)
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            return sdf.format(date)
        }

        /**
         * 将Long时间转成String时间
         *
         * @return yyyy-MM-dd
         */
        fun getStringTimeOfYMD(time: Long?): String {
            val date = Date(time!!)
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            return sdf.format(date)
        }

        val deviceTimeOfSSS: String
            /**
             * 当前的时间(设备)
             *
             * @return yyyy-MM-dd HH:mm:ss:SSS
             */
            get() {
                var date = ""
                date = try {
                    val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")
                    df.format(Date())
                } catch (e: Exception) {
                    e.printStackTrace()
                    Date().time.toString() + "" //当前时间的long字符串
                }
                return date
            }
        val deviceTime: String
            /**
             * 当前的时间(设备)
             *
             * @return yyyy-MM-dd HH:mm:ss
             */
            get() {
                var date = ""
                date = try {
                    val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    df.format(Date())
                } catch (e: Exception) {
                    e.printStackTrace()
                    Date().time.toString() + "" //当前时间的long字符串
                }
                return date
            }
        val deviceTimeOfYMD: String
            /**
             * 当前的时间(年月日)
             *
             * @return yyyy-MM-dd
             */
            get() {
                var date = ""
                try {
                    val df = SimpleDateFormat("yyyy-MM-dd")
                    date = df.format(Date())
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return date
            }
        val deviceTimeOfYM: String
            /**
             * 当前的时间(年月)
             *
             * @return yyyy-MM
             */
            get() {
                var date = ""
                try {
                    val df = SimpleDateFormat("yyyy-MM")
                    date = df.format(Date())
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return date
            }

        /**
         * 获取某月最后一天(年月日)
         *
         * @return yyyy-MM
         */
        fun getLastDayOfMonthOfYMD(year: Int, month: Int): String {
            val cal = Calendar.getInstance()
            // 设置年份
            cal[Calendar.YEAR] = year
            // 设置月份
            cal[Calendar.MONTH] = month - 1
            // 获取某月最大天数
            val lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH)
            // 设置日历中月份的最大天数
            cal[Calendar.DAY_OF_MONTH] = lastDay
            // 格式化日期
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            return sdf.format(cal.time)
        }

        /**
         * 获取某月最后一天(日)
         */
        fun getLastDayOfMonth(year: Int, month: Int): Int {
            val cal = Calendar.getInstance()
            // 设置年份
            cal[Calendar.YEAR] = year
            // 设置月份
            cal[Calendar.MONTH] = month - 1
            // 获取某月最大天数
            return cal.getActualMaximum(Calendar.DAY_OF_MONTH)
        }

        /**
         * 显示日期选择器
         *
         * @param themeLight true 白色背景; false 黑色背景
         */
        fun showDatePickerDialog(
            context: Context?,
            themeLight: Boolean,
            title: String?,
            year: Int,
            month: Int,
            day: Int,
            onDateTimePickerListener: OnDatePickerListener?
        ): BasisTimesUtils {
            var themeId = AlertDialog.THEME_HOLO_LIGHT //默认白色背景
            if (!themeLight) {
                themeId = AlertDialog.THEME_HOLO_DARK //黑色背景
            }
            return showDatePickerDialog(
                context,
                themeId,
                title,
                year,
                month,
                day,
                onDateTimePickerListener
            )
        }

        /**
         * 显示日期选择器, 默认白色背景
         */
        fun showDatePickerDialog(
            context: Context?,
            title: String?,
            year: Int,
            month: Int,
            day: Int,
            onDateTimePickerListener: OnDatePickerListener?
        ): BasisTimesUtils {
            return showDatePickerDialog(
                context,
                AlertDialog.THEME_HOLO_LIGHT,
                title,
                year,
                month,
                day,
                onDateTimePickerListener
            )
        }

        /**
         * 显示日期选择器
         */
        fun showDatePickerDialog(
            context: Context?, themeId: Int, title: String?, year: Int, month: Int, day: Int,
            onDateTimePickerListener: OnDatePickerListener?
        ): BasisTimesUtils {
            mDatePickerDialog = DatePickerDialog(
                context!!, themeId,
                { view, year, month, dayOfMonth ->
                    var month = month
                    month = month + 1 //月份加一
                    onDateTimePickerListener?.onConfirm(year, month, dayOfMonth)
                }, year, month - 1, day
            ) //月份减一
            mDatePickerDialog!!.setOnCancelListener { onDateTimePickerListener?.onCancel() }
            if (!TextUtils.isEmpty(title)) {
                mDatePickerDialog!!.setTitle(title)
            }
            mDatePickerDialog!!.show()
            return BasisTimesUtils()
        }

        /**
         * 显示时间选择器
         */
        fun showTimerPickerDialog(
            context: Context?,
            themeLight: Boolean,
            title: String?,
            hourOfDay: Int,
            minute: Int,
            is24HourView: Boolean,
            onTimerPickerListener: OnTimerPickerListener?
        ) {
            var themeId = AlertDialog.THEME_HOLO_LIGHT //默认白色背景
            if (!themeLight) {
                themeId = AlertDialog.THEME_HOLO_DARK //黑色背景
            }
            showTimerPickerDialog(
                context,
                themeId,
                title,
                hourOfDay,
                minute,
                is24HourView,
                onTimerPickerListener
            )
        }

        /**
         * 显示时间选择器, 默认白色背景
         */
        fun showTimerPickerDialog(
            context: Context?,
            title: String?,
            hourOfDay: Int,
            minute: Int,
            is24HourView: Boolean,
            onTimerPickerListener: OnTimerPickerListener?
        ) {
            showTimerPickerDialog(
                context,
                AlertDialog.THEME_HOLO_LIGHT,
                title,
                hourOfDay,
                minute,
                is24HourView,
                onTimerPickerListener
            )
        }

        /**
         * 显示时间选择器
         */
        fun showTimerPickerDialog(
            context: Context?,
            themeId: Int,
            title: String?,
            hourOfDay: Int,
            minute: Int,
            is24HourView: Boolean,
            onTimerPickerListener: OnTimerPickerListener?
        ) {
            val dialog = TimePickerDialog(context,
                themeId,
                { view, hourOfDay, minute -> onTimerPickerListener?.onConfirm(hourOfDay, minute) },
                hourOfDay,
                minute,
                is24HourView
            )
            dialog.setOnCancelListener { onTimerPickerListener?.onCancel() }
            if (!TextUtils.isEmpty(title)) {
                dialog.setTitle(title)
            }
            dialog.show()
        }
    }
}